package com.askinhand.dao.impl;
// default package

import java.util.Date;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.askinhand.bean.FreshThings;
import com.askinhand.dao.IFreshThingsDAO;

/**
 	* A data access object (DAO) providing persistence and search support for FreshThings entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .FreshThings
  * @author MyEclipse Persistence Tools 
 */

public class FreshThingsDAO extends HibernateDaoSupport implements IFreshThingsDAO  {
	     private static final Logger log = LoggerFactory.getLogger(FreshThingsDAO.class);
		//property constants
	public static final String TYPE = "type";
	public static final String TITLE = "title";
	public static final String CONTENT = "content";
	public static final String UID = "uid";



	protected void initDao() {
		//do nothing
	}
    
    /* (non-Javadoc)
	 * @see IFreshThingsDAO#save(com.askinghand.bean.FreshThings)
	 */
    public void save(FreshThings transientInstance) {
        log.debug("saving FreshThings instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	/* (non-Javadoc)
	 * @see IFreshThingsDAO#delete(com.askinghand.bean.FreshThings)
	 */
	public void delete(FreshThings persistentInstance) {
        log.debug("deleting FreshThings instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    /* (non-Javadoc)
	 * @see IFreshThingsDAO#findById(java.lang.Integer)
	 */
    public FreshThings findById( java.lang.Integer id) {
        log.debug("getting FreshThings instance with id: " + id);
        try {
            FreshThings instance = (FreshThings) getHibernateTemplate()
                    .get("FreshThings", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    /* (non-Javadoc)
	 * @see IFreshThingsDAO#findByExample(com.askinghand.bean.FreshThings)
	 */
    public List findByExample(FreshThings instance) {
        log.debug("finding FreshThings instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    /* (non-Javadoc)
	 * @see IFreshThingsDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding FreshThings instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from FreshThings as model where model." 
         						+ propertyName + "= ?";
		 return getHibernateTemplate().find(queryString, value);
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	/* (non-Javadoc)
	 * @see IFreshThingsDAO#findByType(java.lang.Object)
	 */
	public List findByType(Object type
	) {
		return findByProperty(TYPE, type
		);
	}
	
	/* (non-Javadoc)
	 * @see IFreshThingsDAO#findByTitle(java.lang.Object)
	 */
	public List findByTitle(Object title
	) {
		return findByProperty(TITLE, title
		);
	}
	
	/* (non-Javadoc)
	 * @see IFreshThingsDAO#findByContent(java.lang.Object)
	 */
	public List findByContent(Object content
	) {
		return findByProperty(CONTENT, content
		);
	}
	
	/* (non-Javadoc)
	 * @see IFreshThingsDAO#findByUid(java.lang.Object)
	 */
	public List findByUid(Object uid
	) {
		return findByProperty(UID, uid
		);
	}
	

	/* (non-Javadoc)
	 * @see IFreshThingsDAO#findAll()
	 */
	public List findAll() {
		log.debug("finding all FreshThings instances");
		try {
			String queryString = "from FreshThings";
		 	return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    /* (non-Javadoc)
	 * @see IFreshThingsDAO#merge(com.askinghand.bean.FreshThings)
	 */
    public FreshThings merge(FreshThings detachedInstance) {
        log.debug("merging FreshThings instance");
        try {
            FreshThings result = (FreshThings) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    /* (non-Javadoc)
	 * @see IFreshThingsDAO#attachDirty(com.askinghand.bean.FreshThings)
	 */
    public void attachDirty(FreshThings instance) {
        log.debug("attaching dirty FreshThings instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    /* (non-Javadoc)
	 * @see IFreshThingsDAO#attachClean(com.askinghand.bean.FreshThings)
	 */
    public void attachClean(FreshThings instance) {
        log.debug("attaching clean FreshThings instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

	public static IFreshThingsDAO getFromApplicationContext(ApplicationContext ctx) {
    	return (IFreshThingsDAO) ctx.getBean("FreshThingsDAO");
	}
}